package com.hqully.labmanage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqully.labmanage.common.enums.ResultEnum;
import com.hqully.labmanage.common.exception.DAOException;
import com.hqully.labmanage.common.exception.ResultException;
import com.hqully.labmanage.domain.dto.DeviceDTO;
import com.hqully.labmanage.domain.dto.PageDTO;
import com.hqully.labmanage.domain.entity.Device;
import com.hqully.labmanage.mapper.DeviceMapper;
import com.hqully.labmanage.service.DeviceService;
import com.hqully.labmanage.service.impl.constant.MapperConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 针对数据库表[device]的操作Service
 * </p>
 *
 * @entity {@link Device}
 * @date 2022-10-08 10:05:23 <br>
 * @author hqully <br>
 * @version 1.0
 */
@Slf4j
@Service
public class DeviceServiceImpl implements DeviceService {


    @Resource
    private DeviceMapper deviceMapper;

    @Override
    public PageInfo<DeviceDTO> pageDevices(PageDTO page, DeviceDTO deviceDTO) {
        try {
            PageHelper.startPage(page);
            List<DeviceDTO> deviceList = deviceMapper.listDevices(deviceDTO);
            return new PageInfo<>(deviceList);
        } catch (Exception e) {
            log.error("PAGE [device] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "查询设备分页列表失败");
        }
    }

    @Override
    public List<DeviceDTO> listDevices(DeviceDTO deviceDTO) {
        try {
            return deviceMapper.listDevices(deviceDTO);
        } catch (Exception e) {
            log.error("LIST [device] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "查询设备列表失败");
        }
    }

    @Override
    public int addDevice(DeviceDTO deviceDTO) {
        Device device = new Device();
        BeanUtils.copyProperties(deviceDTO, device);

        int status;
        try {
            status = deviceMapper.addDevice(device);
        } catch (DuplicateKeyException e) {
            log.warn("ADD [device] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceDTO, e.toString());
            throw new ResultException(ResultEnum.DUPLICATE_KEY_CONFLICT, "添加设备失败\n原因: 设备id已存在");
        } catch (Exception e) {
            log.error("ADD [device] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "添加设备失败");
        }
        return status;
    }

    @Override
    public int updateDevice(DeviceDTO deviceDTO) {
        Device device = new Device();
        BeanUtils.copyProperties(deviceDTO, device);

        int status;
        try {
            status = deviceMapper.updateDevice(device);
            if (status == MapperConst.OBJECT_NULL) {
                throw new DAOException("this [device] record not exist");
            }
        }catch (DAOException e){
            log.warn("UPDATE [device] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceDTO, e.toString());
            throw new ResultException(ResultEnum.RESULT_NULL, "更新设备失败\n原因: 设备不存在");
        }
        catch (Exception e) {
            log.error("UPDATE [device] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "更新设备失败");
        }
        return status;
    }

    @Override
    public int deleteDeviceById(String id) {
        int status;
        try {
            status = deviceMapper.deleteDeviceById(id);
            if (status == MapperConst.OBJECT_NULL) {
                throw new DAOException("this [device] record not exist");
            }
        } catch (DAOException e) {
            log.warn("DELETE [device] FAIL\nINPUT OBJECT: {}\nREASON: {}", id, e.toString());
            throw new ResultException(ResultEnum.RESULT_NULL, "删除设备失败\n原因: 设备不存在");
        } catch (Exception e) {
            log.error("DELETE [device] FAIL\nINPUT OBJECT: {}\nREASON: {}", id, e.toString());
            throw new ResultException(ResultEnum.ERROR, "删除设备失败");
        }
        return status;
    }


}



